﻿Public Class frmThemKhachHang
#Region "Public Var"
    Public KhachHangId As Integer = -1
    Public ViewDetail As Boolean = False
#End Region
#Region "Form Event"
    Private Sub frmThemKhachHang_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            TinhThanh_Load()
            NhanVienTiepThi_Load()
            txtCodeKhachHang.Focus()
            txtCodeKhachHang.Text = ""
            txtCodeKhachHang.BackColor = Color.White
            txtTenKhachHang.Text = ""
            txtTenKhachHang.BackColor = Color.White
            txtSoDienThoai.Text = ""
            txtFax.Text = ""
            txtDiaChi.Text = ""
            txtMaSoThue.Text = ""
            txtSoTaiKhoan.Text = ""
            txtNganHang.Text = ""
            txtGhiChu.Text = ""
            If KhachHangId <> -1 Then
                KhachHang_Load(KhachHangId)
            End If
            If ViewDetail Then
                cmdSave.Enabled = False
            Else
                cmdSave.Enabled = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub txtCodeKhachHang_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCodeKhachHang.KeyUp
        Try
            txtCodeKhachHang.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtTenKhachHang_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenKhachHang.KeyUp
        Try
            txtTenKhachHang.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            'Kiem Tra Da Khai Khu Vuc Chua
            If cbxTinhThanh.SelectedValue = Nothing Or cbxTinhThanh.Text = "" Then
                MessageBox.Show("Bạn cần chọn khu vực cho khách hàng này!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                Exit Sub
            End If
            'Thong Tin Khach Hang
            Dim DBO_DMKhachHang As New DanhMucKhachHang
            Dim MaKhachHang As String
            If txtCodeKhachHang.Text = "" Then
                txtCodeKhachHang.Focus()
                txtCodeKhachHang.BackColor = Color.FromArgb(192, 255, 255)
                Exit Sub
            End If
            If txtTenKhachHang.Text = "" Then
                txtTenKhachHang.Focus()
                txtTenKhachHang.BackColor = Color.FromArgb(192, 255, 255)
                Exit Sub
            End If
            If KhachHangId = -1 Then
                'add new
                MaKhachHang = AutoGenKey("DanhMucKhachHang", 5)
                'Kiem Tra Ma Khach Hang Xem Co Trung Khong
                'Kiểm Tra Mã Trùng
                Dim dataAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDb.OleDbCommand
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDb.OleDbCommand("spKhachHang_KiemMaTrung", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@CodeKhachHang", OleDb.OleDbType.VarWChar).Value = txtCodeKhachHang.Text
                If dbCommand.ExecuteScalar() = 1 Then
                    dbConn.Close()
                    MessageBox.Show("Mã khách hàng này đã có trong danh mục.Vui lòng kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    EmptyData(txtCodeKhachHang)
                    Exit Sub
                Else
                    dbConn.Close()
                    DBO_DMKhachHang.Them(MaKhachHang, txtCodeKhachHang.Text, txtTenKhachHang.Text, txtSoDienThoai.Text, txtFax.Text, txtDiaChi.Text, cbxTinhThanh.SelectedValue, txtMaSoThue.Text, txtSoTaiKhoan.Text, txtNganHang.Text, cbxTinhThanh.Text, txtGhiChu.Text, 0, "")
                End If

            Else
                'update 
                DBO_DMKhachHang.ChinhSua(KhachHangId, txtCodeKhachHang.Text, txtTenKhachHang.Text, txtSoDienThoai.Text, txtFax.Text, txtDiaChi.Text, cbxTinhThanh.SelectedValue, txtMaSoThue.Text, txtSoTaiKhoan.Text, txtNganHang.Text, cbxTinhThanh.Text, txtGhiChu.Text, 0, "")
                QuanLyKhachHang.frmDanhMucKhachHang.LoadData_Form()
                Me.Close()
            End If

            txtCodeKhachHang.Text = ""
            txtTenKhachHang.Text = ""
            txtSoDienThoai.Text = ""
            txtFax.Text = ""
            txtDiaChi.Text = ""
            txtMaSoThue.Text = ""
            txtSoTaiKhoan.Text = ""
            txtNganHang.Text = ""
            txtGhiChu.Text = ""
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Xin vui lòng thực hiện lại")
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            If ViewDetail Then
                Me.Close()
            Else
                QuanLyKhachHang.frmDanhMucKhachHang.LoadData_Form()
                Me.Close()
            End If
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Xin vui lòng thực hiện lại")
        End Try
    End Sub
    Private Sub cmdAddNew_TinhThanh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew_TinhThanh.Click
        Try
            ShowModalForm(frmDonViTinh, False, False)
        Catch ex As Exception
            ShowError("Có lỗi trong quá trình xử lý.Vui lòng thực hiện lại", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
#End Region
#Region "Load Data"
    Private Sub KhachHang_Load(ByVal KhachHangId As Integer)
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsTinhThanh As New DataSet
            Dim dtTinhThanh As New DataTable
            Dim query As String = "Select * From DanhMucKhachHang Where KhachHangId=" & KhachHangId
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsTinhThanh)
            dbConn.Close()
            dtTinhThanh = dsTinhThanh.Tables(0)
            '
            txtCodeKhachHang.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("CodeKhachHang")), "", dtTinhThanh.Rows(0)("CodeKhachHang"))
            txtTenKhachHang.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("TenKhachHang")), "", dtTinhThanh.Rows(0)("TenKhachHang"))
            txtSoDienThoai.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("SoDienThoai")), "", dtTinhThanh.Rows(0)("SoDienThoai"))
            txtFax.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("Fax")), "", dtTinhThanh.Rows(0)("Fax"))
            txtDiaChi.Text = dtTinhThanh.Rows(0)("DiaChi")
            cbxTinhThanh.SelectedValue = Integer.Parse(IIf(IsDBNull(dtTinhThanh.Rows(0)("MaTinhThanh")), 0, dtTinhThanh.Rows(0)("MaTinhThanh")))
            txtMaSoThue.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("MaSoThue")), "", dtTinhThanh.Rows(0)("MaSoThue"))
            txtSoTaiKhoan.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("SoTaiKhoan")), "", dtTinhThanh.Rows(0)("SoTaiKhoan"))
            txtNganHang.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("NganHang")), "", dtTinhThanh.Rows(0)("NganHang"))
            txtGhiChu.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("GhiChu")), "", dtTinhThanh.Rows(0)("GhiChu"))
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub TinhThanh_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsTinhThanh As New DataSet
            Dim dtTinhThanh As New DataTable
            Dim query As String = "Select TinhThanhId,TenTinhThanh From TinhThanh Order By TenTinhThanh ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsTinhThanh)
            dbConn.Close()
            dtTinhThanh = dsTinhThanh.Tables(0)
            cbxTinhThanh.DataSource = dtTinhThanh
            cbxTinhThanh.DisplayMember = "TenTinhThanh"
            cbxTinhThanh.ValueMember = "TinhThanhId"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub NhanVienTiepThi_Load()
        Try
            'Dim dbConn As New OleDb.OleDbConnection
            'Dim dataAccess As New DataAccess
            'Dim dsNhanVienTiepThi As New DataSet
            'Dim dtNhanVienTiepThi As New DataTable
            'Dim query As String = "Select NhanVienTiepThiId,TenNhanVienTiepThi From NhanVienTiepThi Union Select pkey as NhanVienTiepThi,[name] as TenNhanVienTiepThi From KhongXacDinh Order By NhanVienTiepThiId ASC"
            'Dim adapter As OleDb.OleDbDataAdapter
            'dataAccess.ConnectDatabase_DBAccess(dbConn)
            'adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            'adapter.Fill(dsNhanVienTiepThi)
            'dbConn.Close()
            'dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            'cbxNhanVienTiepThi.DataSource = dtNhanVienTiepThi
            'cbxNhanVienTiepThi.ValueMember = "NhanVienTiepThiId"
            'cbxNhanVienTiepThi.DisplayMember = "TenNhanVienTiepThi"

        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region

    
    
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        ' if it is a hotkey, return true; otherwise, return false
        Select Case keyData
            Case Keys.Escape
                CloseForm()
            Case Else
                Exit Function
        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
#Region "Hot Key"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Đóng Form
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 10/03/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Function CloseForm() As Boolean
        Try
            Me.Close()
        Catch ex As Exception
            Throw
        End Try
    End Function
#End Region
End Class
